#pragma once
#include <sys/types.h>
#include <iostream>
#include "Socket.hpp"
#include "Reactor.hpp"

class Reactor;
class Connection
{
public:
    Connection() : _sockfd(-1), _events(0)
    {
    }
    void SetInet(const InetAddr &client) { _peer_client = client; }
    void Setfd(int sockfd) { _sockfd = sockfd; }
    int Fd() { return _sockfd; }
    void SetOwner(Reactor *owner) { _owner = owner; }
    void SetEvent(uint32_t event) { _events = event; }
    uint32_t GetEvents() { return _events; }
    Reactor *GetOwner() { return _owner; }

    //将内容添加至接收缓冲区
    void AppendInbuffer(const std::string &in) { _inbuffer += in; }
    //将内容添加至输出缓冲区
    void AppendOutbuffer(const std::string &out) { _outbuffer += out; }

    void DisCardOutString(int n) { _outbuffer.erase(0, n); }
    bool IsOutBufferEmpty() { return _outbuffer.empty(); }

    std::string &OutString() // 调出缓冲区
    {
        return _outbuffer;
    }
    std::string &InBuffer() 
    {
        return _inbuffer;
    }
    void Close()
    {
        if (_sockfd > 0)
            close(_sockfd);
    }
    ~Connection()
    {
       Close();
    }

    //设置给IO模块的接口
    virtual void Recver() = 0;
    virtual void Sender() = 0;
    virtual void Excepter() = 0;
protected:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;
    InetAddr _peer_client;

    // 关心的事件
    uint32_t _events;
    // 回调
    Reactor *_owner;
};